package com.saver.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.saver.R;
import com.saver.view.gui.CircleView;

import java.util.List;

public class PieListCategoriaAdapter extends ArrayAdapter<ContentItemListCategoria> {

    public PieListCategoriaAdapter(Context context, List<ContentItemListCategoria> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ContentItemListCategoria c = getItem(position);

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_chart, null);
            holder.icono = (CircleView) convertView.findViewById(R.id.icon_circle_category);
            holder.descripcion = (TextView) convertView.findViewById(R.id.nombre_categoria_pie);
            holder.monto = (TextView) convertView.findViewById(R.id.monto_pie);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (c.icono != null) {
            String[] rgb = c.color.split(",");
            int color = Color.rgb(Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2]));
            holder.icono.setTitleText(c.descrpcion.substring(0, 1).toUpperCase());
            holder.icono.setStrokeColor(color);
            holder.icono.setTitleColor(color);
            holder.icono.setFillColor(color & 0x60FFFFFF);
            holder.descripcion.setText(c.descrpcion.trim().toUpperCase());
            holder.monto.setText(c.monto);
            holder.id = c.id;
            holder.value = c.montoValue;
        }

        return convertView;
    }

    public class ViewHolder {
        public TextView descripcion, monto;
        public Integer id;
        public Float value;
        public CircleView icono;
    }
}
